from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from po import settings
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException,NoSuchElementException


class BasePage:
    def __init__(self,driver,url):
        self._driver = driver
        self._url = url



    def open(self):
        self._driver.get(url = self._url)

    def find_element(self,*locator,timeout=None):
        try:
            self._wait(timeout).until(EC.visibility_of_element_located(locator=locator))
        except (TimeoutException,NoSuchElementException):
            self._driver.quit()
            raise TimeoutException("定位元素失败方式为{}".format(locator))


    def _wait(self,timeout):
        if timeout is None:
            return  WebDriverWait(driver = self._driver,timeout=settings.UI_WAIT_TIME)
        else:
            return WebDriverWait(driver = self._driver,timeout=timeout)

    def send_key(self,webElement,keys):
        webElement.clear()
        webElement.send_keys(keys)

